<template>
    <div>
        <h4 class="lighter">
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            <router-link to="/business/course" class="pink"> {{course.name}} </router-link>：
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            <router-link to="/business/chapter" class="pink"> {{chapter.name}} </router-link>
        </h4>
        <hr>
        <p>
            <router-link to="/business/chapter" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-arrow-left"></i>
                返回大章
            </router-link>
            <!--模态框(Modal)插件的触发按钮-->
            <button v-on:click="add()" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-edit"></i>
                新增
            </button>
            &nbsp;
            <!--刷新按钮-->
            <button v-on:click="getList(1)" class="btn btn-white btn-default btn-round">
                <i class="ace-icon fa fa-refresh"></i>
                刷新
            </button>
        </p>
        <!--pagination可选参数：v-bind:itemCount="8" 不设置默认显示5个按钮，设置最多显示8个按钮-->
        <pagination ref="pagination" v-bind:list="getList"></pagination>

        <table id="simple-table" class="table  table-bordered table-hover">
                <thead>
                <tr>
                    <th>标题</th>
                    <th>videoId</th>
                    <th>时长</th>
                    <th>收费</th>
                    <th>顺序</th>
                    <th>操作</th>
                </tr>
                </thead>

                <tbody>

                <tr v-for="section in sections">
                    <td>{{section.title}}</td>
                    <td>{{section.vod}}</td>
                    <td>{{section.time | formatSecond}}</td>
                    <td>{{CHARGE | optionKV(section.charge)}}</td>
                    <td>{{section.sort}}</td>
                    <td>
                        <div class="hidden-sm hidden-xs btn-group">

                            <button  v-on:click="authPlay(section)" class="btn btn-xs btn-info">
                                <i class="ace-icon fa fa-video-camera bigger-120"></i>
                            </button>

                            <button  v-on:click="edit(section)" class="btn btn-xs btn-info">
                                <i class="ace-icon fa fa-pencil bigger-120"></i>
                            </button>

                            <button v-on:click="del(section.id)" class="btn btn-xs btn-danger">
                                <i class="ace-icon fa fa-trash-o bigger-120"></i>
                            </button>

                        </div>

                        <div class="hidden-md hidden-lg">
                            <div class="inline pos-rel">
                                <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
                                    <i class="ace-icon fa fa-cog icon-only bigger-110"></i>
                                </button>

                                <ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
                                    <li>
                                        <a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <!--摸态框插件的实体html-->
        <div id="form-modal" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">表单</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">标题</label>
                                <div class="col-sm-10">
                                    <input v-model="section.title" class="form-control" placeholder="标题">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">课程</label>
                                <div class="col-sm-10">
                                    <p class="form-control-static">{{course.name}}</p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">大章</label>
                                <div class="col-sm-10">
                                    <p class="form-control-static">{{chapter.name}}</p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">视频</label>
                                <div class="col-sm-10">
                                    <vod v-bind:input-id="'video-upload'"
                                          v-bind:text="'上传视频点播服务'"
                                          v-bind:suffixs="['mp4']"
                                          v-bind:use="FILE_USE.COURSE.key"
                                          v-bind:after-upload="afterUpload"
                                          v-bind:shard-size="vod.shardSize"
                                          v-bind:url="vod.url">
                                    </vod>
                                    <div v-show="section.video" class="row">
                                        <div class="col-md-9">
                                            <player v-bind:player-id="'player-div'"
                                                    ref="player">
                                            </player>
                                        </div>
                                        <div class="col-md-9">
                                            <video v-bind:src="section.video" id="video" controls="controls" class="hidden"></video>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">时长</label>
                                <div class="col-sm-10">
                                    <input v-model="section.time" class="form-control" placeholder="时长">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">视频</label>
                                <div class="col-sm-10">
                                    <input v-model="section.video" class="form-control" disabled>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">VOD</label>
                                <div class="col-sm-10">
                                    <input v-model="section.vod" class="form-control" disabled>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">收费</label>
                                <div class="col-sm-10">
                                    <select v-model="section.charge" class="form-control">
                                        <option v-for="o in CHARGE" v-bind:value="o.key">{{o.value}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">顺序</label>
                                <div class="col-sm-10">
                                    <input v-model="section.sort" class="form-control" placeholder="顺序">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="save" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

        <auth-player v-bind:player-id="'authPlay-div'"
                     v-bind:auth-div="'authJ_prismPlayer'"
                     v-bind:url="vod.url"
                     ref="authPlayer">
        </auth-player>

    </div>
</template>

<script>
    import Pagination from "../../components/pagination";
    import OssFile from "../../components/oss-file";
    import Vod from "../../components/vod";
    import Player from "../../components/player";
    import AuthPlayer from "../../components/auth-player";
    export default {
        components: {AuthPlayer, Player, Pagination, OssFile, Vod},
        name: "business-section",
        data: function() {
            return {
                section: {},
                sections: [],
                CHARGE: SECTION_CHARGE,
                FILE_USE: FILE_USE,
                course: {},
                chapter: {},
                vod: {shardSize: 5000*1024*1024,url: 'vod'},
            }
        },
        mounted: function() {
            // sidebar激活样式方法一
            this.$parent.activeSidebar("business-section-sidebar");
            let _this = this;

            _this.$refs.pagination.size = 10;

            let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
            let chapter = SessionStorage.get(SESSION_KEY_CHAPTER) || {};
            if (Tool.isEmpty(course) || Tool.isEmpty(chapter)) {
                _this.$router.push("/welcome");
            }
            _this.course = course;
            _this.chapter = chapter;

            _this.getList(1);

            // 页面初始化时加载#form-modal模态框关闭事件
            $('#form-modal').on('hidden.bs.modal', function (e) {
                // 模态框关闭时执行player组件的pause()方法暂停视频。
                _this.$refs.player.pause();
            });
        },
        methods: {
            /**
             * 点击【新增】
             */
            add:function() {
                let _this = this;
                _this.section = {};
                $("#form-modal").modal("show");
            },
            /**
             * 点击【编辑】
             */
            edit:function(section) {
                let _this = this;
                _this.section = $.extend({}, section);
                $("#form-modal").modal("show");
            },
            /**
             * 列表查询
             */
            getList: function (page) {
                let _this = this;
                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/section/list',{
                    page: page,
                    size: _this.$refs.pagination.size,
                    courseId: _this.course.id,
                    chapterId: _this.chapter.id
                }).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    _this.sections = resp.content.list;
                    _this.$refs.pagination.render(page, resp.content.total);

                })

            },
            /**
             * 点击【保存】
             */
            save:function(page) {
                let _this = this;
                _this.section.video = "";//将video设置为空，不检查。
                //保存校验
                if (1 != 1
                    || !Validator.require(_this.section.title, "标题")
                    || !Validator.length(_this.section.title, "标题", 1, 50)
                    || !Validator.length(_this.section.video, "视频", 1, 200)
                ) {
                    return;
                }
                _this.section.courseId = _this.course.id;
                _this.section.chapterId = _this.chapter.id;

                Loading.show();
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/section/save', _this.section).then((response)=>{
                    Loading.hide();
                    let resp = response.data;
                    if (resp.success) {
                        $("#form-modal").modal("hide");
                        _this.getList(1);
                        Toast.success("保存成功！");
                    }
                })
            },
            /**
             * 点击【删除】
             */
            del(id) {
                let _this = this;
                Confirm.show("删除小节后不可恢复，确认删除？", function () {
                    Loading.show();
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/section/delete/' + id).then((response)=>{
                        Loading.hide();
                        let resp = response.data;
                        if (resp.success) {
                            _this.getList(1);
                            Toast.success("删除成功！");
                        }
                    })
                });
            },
            afterUpload: function (resp) {
                let _this = this;
                let video = resp.content.path;
                let vod = resp.content.vod;
                _this.section.video = video;
                _this.$forceUpdate();
                _this.section.vod = vod;
                //_this.$refs.pagination.render(page, resp.content.total);
                _this.$refs.player.play(video);
                _this.getTime();
            },

            /**
             * 获取时长
             */
            getTime() {
                let _this = this;
                setTimeout(function () {
                    let ele = document.getElementById("video");
                    _this.section.time = parseInt(ele.duration, 10);
                },1000);
            },

            /**
             * 点击获取授权并播放视频
             * @param section
             */
            authPlay:function (section) {
                let _this = this;
                _this.$refs.authPlayer.clickAuthPlay(section)
            },
        }
    }
</script>


<style scoped>
    video {
        width: 100%;
        height: auto;
        margin-top: 10px;
    }
</style>